"""
Case Type   : 资源池化集群系统支持归档备份
Case Name   : 设置archive_mode为on时，设置archive_dest无权限目录
Create At   : 2024/04/19
Owner       : @kyleze
Description :
    1.设置archive_mode=on
    2.设置archive_dest='/root/archive_dir'
    3.执行pg_switch_xlog
    4.查看归档文件是否存在
Expect      :
    1.成功
    2.成功
    3.成功
    4.不生成归档文件
History     :
"""

import os
import unittest

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH

from yat.test import Node
from yat.test import macro


@unittest.skipIf(
    "dms_res" not in Common().get_sh_result(
        Node("PrimaryDbUser"),
        f"source {macro.DB_ENV_PATH};cm_ctl query -Cvidp"), "非资源池化不执行")
class SharedStorageAchiveXlog(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"----{os.path.basename(__file__)} start----")
        self.constant = Constant()
        self.com = Common()
        self.primary_sh = CommonSH("PrimaryDbUser")
        self.primary_node = Node("PrimaryDbUser")
        self.default_archive_mode = \
            self.com.show_param("archive_mode")
        self.default_archive_dest = \
            self.com.show_param("archive_dest")

    def test_archive_xlog(self):
        step = "--step2:设置archive_mode=on expect:成功--"
        self.logger.info(step)
        guc_res = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_mode=on")
        self.logger.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)

        step = "--step2:设置archive_dest='/root/archive_dir' expect:成功--"
        self.logger.info(step)
        guc_res = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_dest='/root/archive_dir'")
        self.logger.info(guc_res)
        self.assertTrue(guc_res, "执行失败:" + step)

        step = "--step3:执行pg_switch_xlog expect:成功--"
        self.logger.info(step)
        result = self.primary_sh.execut_db_sql(
            "select pg_switch_xlog();")
        self.logger.info(result)
        self.assertIn('1 row', result, "执行失败:" + step)

        step = "--step4:查看归档文件是否存在 expect:不生成归档文件--"
        self.logger.info(step)
        cmd = f"ls -l /root/archive_dir"
        self.logger.info(cmd)
        ls_archive = self.primary_node.sh(cmd).result()
        self.logger.info(ls_archive)
        self.assertIn("Permission denied", ls_archive, "执行失败:" + step)

    def tearDown(self):
        self.logger.info("----清理环境----")
        guc_res1 = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_mode={self.default_archive_mode}")
        self.logger.info(guc_res1)
        guc_res2 = self.primary_sh.execute_gsguc(
            "reload", self.constant.GSGUC_SUCCESS_MSG,
            f"archive_dest='{self.default_archive_dest}'")
        self.logger.info(guc_res2)
        self.assertTrue(guc_res1, "设置参数失败")
        self.assertTrue(guc_res2, "设置参数失败")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
